{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "89333304",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[RangeIndex(start=0, stop=1590, step=1), Index(['ts_code', 'name', 'management', 'custodian', 'fund_type', 'found_date',\n",
      "       'due_date', 'list_date', 'issue_date', 'delist_date', 'issue_amount',\n",
      "       'm_fee', 'c_fee', 'duration_year', 'p_value', 'min_amount',\n",
      "       'exp_return', 'benchmark', 'status', 'invest_type', 'type', 'trustee',\n",
      "       'purc_startdate', 'redm_startdate', 'market'],\n",
      "      dtype='object')]\n",
      "        ts_code       name management custodian fund_type found_date  \\\n",
      "0     159613.SZ    信息安全ETF       嘉实基金    中国建设银行       股票型   20220119   \n",
      "1     159738.SZ  沪港深云计算ETF     华泰柏瑞基金    中国建设银行       股票型   20220118   \n",
      "2     513230.SH    港股消费ETF       华夏基金      交通银行       股票型   20220112   \n",
      "3     562950.SH  消费电子50ETF      易方达基金      浦发银行       股票型   20220112   \n",
      "4     159775.SZ  新能源车电池ETF       建信基金      中信证券       股票型   20220107   \n",
      "...         ...        ...        ...       ...       ...        ...   \n",
      "1585  500019.SH       基金普润       鹏华基金    中国工商银行       股票型   19920509   \n",
      "1586  500013.SH       基金安瑞       华安基金    中国工商银行       股票型   19920429   \n",
      "1587  500017.SH       基金景业       大成基金    中国农业银行       股票型   19920401   \n",
      "1588  184702.SZ       基金同智       长盛基金      中国银行       股票型   19920313   \n",
      "1589  500028.SH       基金兴业       华夏基金    中国农业银行       股票型   19911115   \n",
      "\n",
      "      due_date list_date issue_date delist_date  ...  min_amount  exp_return  \\\n",
      "0         None  20220127   20220110        None  ...         0.1        None   \n",
      "1         None  20220128   20211022        None  ...         0.1        None   \n",
      "2         None  20220124   20211130        None  ...         0.1        None   \n",
      "3         None  20220120   20211130        None  ...         0.1        None   \n",
      "4         None  20220124   20211201        None  ...         0.1        None   \n",
      "...        ...       ...        ...         ...  ...         ...         ...   \n",
      "1585  20070424  20010904       None    20070425  ...         NaN        None   \n",
      "1586  20070409  20010830       None    20070410  ...         NaN        None   \n",
      "1587  20070115  20011219       None    20070116  ...         NaN        None   \n",
      "1588  20070104  20000515       None    20070105  ...         NaN        None   \n",
      "1589  20060808  20010727       None    20060809  ...         NaN        None   \n",
      "\n",
      "                    benchmark  status  invest_type    type trustee  \\\n",
      "0               中证信息安全主题指数收益率       I        被动指数型  契约型开放式    None   \n",
      "1             中证沪港深云计算产业指数收益率       I        被动指数型  契约型开放式    None   \n",
      "2     经估值汇率调整后的中证港股通消费主题指数收益率       L        被动指数型  契约型开放式    None   \n",
      "3               中证消费电子主题指数收益率       L        被动指数型  契约型开放式    None   \n",
      "4               国证新能源车电池指数收益率       L        被动指数型  契约型开放式    None   \n",
      "...                       ...     ...          ...     ...     ...   \n",
      "1585                     None       D          成长型  契约型封闭式    None   \n",
      "1586                     None       D          成长型  契约型封闭式    None   \n",
      "1587                     None       D          成长型  契约型封闭式    None   \n",
      "1588                     None       D          成长型  契约型封闭式    None   \n",
      "1589                     None       D          平衡型  契约型封闭式    None   \n",
      "\n",
      "     purc_startdate redm_startdate market  \n",
      "0          20220127       20220127      E  \n",
      "1          20220128       20220128      E  \n",
      "2          20220124       20220124      E  \n",
      "3          20220120       20220120      E  \n",
      "4          20220124       20220124      E  \n",
      "...             ...            ...    ...  \n",
      "1585       20010904           None      E  \n",
      "1586       20010830           None      E  \n",
      "1587       20011219           None      E  \n",
      "1588       20000515           None      E  \n",
      "1589       20010727           None      E  \n",
      "\n",
      "[1590 rows x 25 columns]\n"
     ]
    }
   ],
   "source": [
    "import tushare as ts\n",
    "import json\n",
    "\n",
    "def obj_to_Json(obj):\n",
    "    data = json.dumps(obj, indent=4,ensure_ascii=False)\n",
    "    fh = open('data.json', 'w')\n",
    "    fh.write(data)\n",
    "    fh.close()  # 最终写入的json文件格式:\n",
    "\n",
    "#print(ts.__version__)\n",
    "#ts.set_token('dfeb543866c1b2b3e804f3cbed560e9e1709b0d06cd8182e1cc5676d')\n",
    "ts_pro = ts.pro_api('ea3263c5424f08c3e04d605af9458cfc349613d5b7c27e99994eb396')\n",
    "df = ts_pro.fund_basic(market = 'E')\n",
    "print(df.axes)\n",
    "df_as_json=df.to_json(orient='records', lines=True)\n",
    "\n",
    "df.to_json(orient='records',lines=True,path_or_buf=\"fund_data.json\")\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e991a98c",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'df_as_json' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m/var/folders/mh/l6_b7c0x7m3bq41r5m25snqc0000gn/T/ipykernel_56583/673861873.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0mbulk_data\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0mnum\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mjson_document\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mdf_as_json\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'\\n'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      6\u001b[0m     \u001b[0mnum\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnum\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m     \u001b[0;31m#print(json_document)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'df_as_json' is not defined"
     ]
    }
   ],
   "source": [
    "from elasticsearch import Elasticsearch\n",
    "es = Elasticsearch('http://localhost:9200')\n",
    "bulk_data = []\n",
    "num=0\n",
    "for json_document in df_as_json.split('\\n'):\n",
    "    num=num+1\n",
    "    #print(json_document)\n",
    "    if json_document=='':\n",
    "        print(\"json:%d,%s|\" %(num,json_document))\n",
    "        break\n",
    "    bulk_data.append({\"index\":{}})\n",
    "    bulk_data.append(json.loads(json_document))\n",
    "    #bulk_data.append(json_document)\n",
    "            \n",
    "    # 一次bulk request包含1000条数据\n",
    "    if len(bulk_data) >= 1000:\n",
    "        es.bulk(index=\"fund_basic\", body=bulk_data)\n",
    "        bulk_data = []\n",
    "        #break\n",
    "#df.length()\n",
    "print(len(bulk_data))\n",
    "es.bulk(index=\"fund_basic\",body=bulk_data)\n",
    "\n",
    "obj_to_Json(bulk_data)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a0150f19",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'CatClient' object is not callable",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m/var/folders/mh/l6_b7c0x7m3bq41r5m25snqc0000gn/T/ipykernel_56583/2815969035.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0melasticsearch\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mElasticsearch\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0mes\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mElasticsearch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'http://localhost:9200'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mes\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: 'CatClient' object is not callable"
     ]
    }
   ],
   "source": [
    "from elasticsearch import Elasticsearch\n",
    "es = Elasticsearch('http://localhost:9200')\n",
    "es.cat()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "54098a0c",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name '__file__' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m/var/folders/mh/l6_b7c0x7m3bq41r5m25snqc0000gn/T/ipykernel_15464/593581250.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrealpath\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m__file__\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name '__file__' is not defined"
     ]
    }
   ],
   "source": [
    "import os\n",
    "print(os.path.split(os.path.realpath(__file__))[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bcdf0d9d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
